package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteMapper routeMapper;

    /**
     * 根据线路id查询线路
     */
    @Override
    public RouteVo findRouteById(RouteVo routeVo) {
        Long id = routeVo.getId();
        if(EmptyUtil.isNullOrEmpty(id)){
            return null;
        }
        Route route = routeMapper.selectByPrimaryKey(id);
        return BeanConv.toBean(route,RouteVo.class);
    }

    /**
     *分页查询线路
     */
    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        RouteExample routeExample = new RouteExample();
        RouteExample.Criteria criteria = routeExample.createCriteria();
        if(!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getFlag())){
           criteria.andFlagEqualTo(routeVo.getFlag());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getIsThemeTour())){
            criteria.andIsThemeTourEqualTo(routeVo.getIsThemeTour());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getRouteIntroduce())){
            criteria.andRouteIntroduceLike("%"+routeVo.getRouteIntroduce()+"%");
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%"+routeVo.getRouteName()+"%");
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        List<Route> routelist = routeMapper.selectByExample(routeExample);
        if(EmptyUtil.isNullOrEmpty(routelist)){
            return null;
        }
        PageInfo<Route> routePageInfo = new PageInfo<>(routelist);
        PageInfo<RouteVo> routeVoPageInfo = new PageInfo<>();
        //浅层拷贝
        BeanConv.toBean(routePageInfo,routeVoPageInfo);
        //深层拷贝
        List<RouteVo> routeVoList = BeanConv.toBeanList(routePageInfo.getList(), RouteVo.class);
        routeVoPageInfo.setList(routeVoList);
        return routeVoPageInfo;
    }
}
